package com.yygo.dao.hibernate;

import com.yygo.Constants;
import com.yygo.dao.PrepaymentDao;
import com.yygo.model.LoanRequest;
import com.yygo.model.Prepayment;
import com.yygo.model.User;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by tiansha on 2015/11/7.
 */
@Repository("prepaymentDao")
public class PrepaymentDaoHibernate extends GenericDaoHibernate<Prepayment, Long> implements PrepaymentDao {

    public PrepaymentDaoHibernate() {
        super(Prepayment.class);
    }

    @Override
    public List<Prepayment> getPendingPrepayments(User user) {
        Criteria criteria = getSession().createCriteria(Prepayment.class);
        criteria.createCriteria("loanRequest").add(Restrictions.eq("canPrepay", true)).createCriteria("loanProduct").add(Restrictions.eq("bank", user.getBank()));
        List prepayments = criteria.list();
        return prepayments;
    }

    @Override
    public Prepayment getPrepayment(LoanRequest loanRequest) {
        Criteria criteria = getSession().createCriteria(Prepayment.class).add(Restrictions.eq("loanRequest", loanRequest));
        return (Prepayment)criteria.uniqueResult();
    }
}
